using System.Linq;
using System.Numerics;
using NUnit.Framework;

namespace ProjectEuler.Core
{
    public class Problem63 : IProjectEulerProblem
    {
        public int Number
        {
            get { return 63; }
        }

        public string Description
        {
            get { return "How many n-digit positive integers exist which are also an nth power?"; }
        }

        public string Answer
        {
            get { return GetCountOfPowerDigits().ToString(); }
        }

        protected bool IsPowerDigits(int num, int power)
        {
            var value = BigInteger.Pow(num, power);
            return (value.ToString().Length == power);
        }

        protected int GetCountOfPowerDigits()
        {
            // the problem states "positive integers", and 0 is not a positive integer
            // the ranges of "100" was arbitrarily selected, but seems to work
            var nums = Enumerable.Range(1, 100);
            var powers = Enumerable.Range(1, 100);
            return (from p in powers
                    from n in nums
                    where IsPowerDigits(n, p)
                    select 1).Count();
        }
    }

    [TestFixture]
    public class Problem63Tests : Problem63
    {
        [Test]
        public void Can_tell_if_a_number_is_a_power_with_the_same_digits()
        {
            Assert.That(IsPowerDigits(7, 5));
            Assert.That(IsPowerDigits(8, 9));
        }
    }
}